using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Microsoft.Win32;

namespace eBirdAttend
{
    public partial class ShiftDetails : UserControl
    {

        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();
        String strBtnCaption = "";

        public ShiftDetails()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
        }

        // Function name   : btnAdd_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnAdd_Click(object sender, EventArgs e)
        {  
            ClearControls();
            cmbEndAMPM.SelectedIndex = 0;
            cmbStartAMPM.SelectedIndex = 0;
            groupBox1.Visible = true;
            strBtnCaption = "Add";
            groupBox1.Text = "ADD SHIFT";
         
            try
            {
                sqlCmd.CommandText = "select max(shid) from shiftdetails";
                sqlCmd.Connection = sqlCon;
                sqlCon.Open();
                SqlDataReader sqlReader;
                sqlReader = sqlCmd.ExecuteReader();
                if (sqlReader.Read())
                {
                    if (sqlReader[0].ToString() == "")
                    {
                    }
                    else
                    {
                        txtShiftId.Text = Convert.ToString(Convert.ToInt32(sqlReader[0].ToString()) + 1);
                    }
                }
                else
                {
                    txtShiftId.Text = "1";
                }

                if (txtShiftId.Text == "")
                {
                    txtShiftId.Text = "1";
                }
                txtShiftName.Focus();
            }
            catch (Exception e1)
            {

            }
            finally
            {
                sqlCon.Close();
            }
        }

        private void ShiftDetails_Load(object sender, EventArgs e)
        {
            try
            {                

                string _strc = Properties.Settings.Default["ConnectionString"].ToString();
                shiftDetailsTableAdapter.Connection.ConnectionString = _strc;
                //shiftDetailsTableAdapter.Connection.ConnectionString = Properties.Settings.Default["ConString"].ToString();
                this.shiftDetailsTableAdapter.Fill(this.shiftDetailsDataset.ShiftDetails);
                //this.shiftDetailsTableAdapter1.Connection.ConnectionString = Properties.Settings.Default["ConString"].ToString();
                //this.shiftDetailsTableAdapter1.Fill(this.shiftDetailsDataSet.ShiftDetails);
                cmbStartAMPM.SelectedIndex = 0;
                cmbEndAMPM.SelectedIndex = 0;
                cmbStartHr.SelectedIndex = 0;
                cmbStartMin.SelectedIndex = 0;
                cmbEndHr.SelectedIndex = 0;
                cmbEndMin.SelectedIndex = 0;
                shiftView.Columns[0].Visible = false;
            }
            catch (Exception ex)
            {
            }
        }

        // Function name   : btnEdit_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (shiftView.SelectedRows.Count == 0)
            { MessageBox.Show("Please select a row to edit"); return; }

            if ((shiftView.RowCount - 1) == shiftView.SelectedRows[0].Index)
            {
                MessageBox.Show("No content to edit");
                groupBox1.Visible = false;
                ClearControls();
                return;
            }
            ClearControls();
            groupBox1.Visible = true;
            strBtnCaption = "Edit";
            groupBox1.Text = "EDIT SHIFT";          
           
            //MessageBox.Show(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
            txtShiftId.Text = shiftView.SelectedRows[0].Cells[0].Value.ToString();
            txtShiftName.Text = shiftView.SelectedRows[0].Cells[1].Value.ToString();
            txtShiftDesc.Text = shiftView.SelectedRows[0].Cells[2].Value.ToString();
            DateTime StartTime = Convert.ToDateTime(shiftView.SelectedRows[0].Cells[3].Value.ToString());
           
            if (StartTime.Hour > 12)
            {
                cmbStartHr.Text = Convert.ToString(StartTime.Hour - 12);
            }
            else
            {
                cmbStartHr.Text = StartTime.Hour.ToString();
            }
            cmbStartMin.Text = StartTime.Minute.ToString();
            if (shiftView.SelectedRows[0].Cells[3].Value.ToString().Substring(shiftView.SelectedRows[0].Cells[3].Value.ToString().Length - 2, 2) == "PM")
            {
                cmbStartAMPM.SelectedIndex = 1;
            }
            else
            {
                cmbStartAMPM.SelectedIndex = 0;
            }
            DateTime EndTime = Convert.ToDateTime(shiftView.SelectedRows[0].Cells[4].Value.ToString());
            if (EndTime.Hour > 12)
            {
                cmbEndHr.Text = Convert.ToString(EndTime.Hour - 12);
            }
            else
            {
                cmbEndHr.Text = EndTime.Hour.ToString();
            }
            cmbEndMin.Text = EndTime.Minute.ToString();
            if (shiftView.SelectedRows[0].Cells[4].Value.ToString().Substring(shiftView.SelectedRows[0].Cells[4].Value.ToString().Length - 2, 2) == "PM")
            {
                cmbEndAMPM.SelectedIndex = 1;
            }
            else
            {
                cmbEndAMPM.SelectedIndex = 0;
            }
            txtGraceTime.Text = shiftView.SelectedRows[0].Cells[5].Value.ToString();
            txtMinHalf.Text = shiftView.SelectedRows[0].Cells[6].Value.ToString();
            txtMaxHalf.Text = shiftView.SelectedRows[0].Cells[7].Value.ToString();
            txtMaxTime.Text = shiftView.SelectedRows[0].Cells[9].Value.ToString();
            txtShiftName.Focus();
        }


        // Function name   : btnDelete_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (shiftView.SelectedRows.Count == 0)
                { MessageBox.Show("Please select a row to delete"); return; }

                if (shiftView.RowCount != 1)
                {
                    DialogResult _dlg = MessageBox.Show("Are you sure you want to delete?", "Warning", MessageBoxButtons.YesNo);
                    if (_dlg == DialogResult.No)
                    {
                        return;
                    }
                    else
                    {
                        string strShiftId = shiftView.SelectedRows[0].Cells[0].Value.ToString();
                        while (shiftView.Rows.Count > 1)
                            shiftView.Rows.RemoveAt(0);
                        sqlCmd.CommandText = "delete from  shiftdetails where shid ='" + strShiftId + "'";
                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();
                        sqlCon.Close();
                        this.shiftDetailsTableAdapter.Fill(this.shiftDetailsDataset.ShiftDetails);
                        MessageBox.Show("Successfuly Deleted");
                    }
                }
                else
                {
                    MessageBox.Show("No data found");
                }
            }
            catch (Exception ex)
            {
            }
        }

        // Function name   : btnCancel_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            ClearControls();
        }

        // Function name   : ClearControls
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/19/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        
        public void ClearControls()
        {
            txtShiftId.Text = "";
            txtShiftName.Text = "";
            txtShiftDesc.Text = "";
            cmbStartHr.SelectedIndex = 0;
            cmbStartMin.SelectedIndex = 0;
            cmbEndHr.SelectedIndex = 0;
            cmbEndMin.SelectedIndex = 0;
            cmbStartAMPM.SelectedIndex = 0;
            cmbEndAMPM.SelectedIndex = 0;
            txtGraceTime.Text = "";
            txtMinHalf.Text = "";
            txtMaxHalf.Text = "";
            groupBox1.Visible = false;
        }

        // Function name   : btnSave_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            DateTime  StartDate;
            DateTime EndDate;
            String strAMPM = "";
            if(cmbStartAMPM.SelectedIndex == 0)
            {
              strAMPM = "AM";
            }
            else
            {
              strAMPM = "PM";
            }

            string strStartDate = cmbStartHr.Text + ":" + cmbStartMin.Text + ":00 " + strAMPM;
            
            StartDate = Convert.ToDateTime(strStartDate);

            if (cmbEndAMPM.SelectedIndex == 0)
            {
                strAMPM = "AM";
            }
            else
            {
                strAMPM = "PM";
            }

            string strEndDate = cmbEndHr.Text + ":" + cmbEndMin.Text + ":00 " + strAMPM;
            EndDate = Convert.ToDateTime(strEndDate);

           if ((txtShiftName.Text  == "") || (txtMaxTime.Text == ""))
           {
               MessageBox.Show("Please enter the required fields");
               txtShiftName.Focus();
           }
           else
           {
               //insert the new shift details              

               if (strBtnCaption == "Add")
               {
                   int iOverlapped = 0;
                   if (StartDate.TimeOfDay > EndDate.TimeOfDay) iOverlapped = 1;                   
                   while (shiftView.Rows.Count > 1)
                       shiftView.Rows.RemoveAt(0);
                   sqlCmd.CommandText = "insert into shiftdetails (ShName,ShDesc,ShStartTime,ShEndTime,ShGraceTime,ShMinTimeFrHalfDay,ShMinTimeFrFullDay,shOverLapped,ShMaxTime)values('"
                                        + txtShiftName.Text + "','" + txtShiftDesc.Text
                                        + "','" + StartDate.TimeOfDay + "','"
                                        + EndDate.TimeOfDay + "','"
                                        + txtGraceTime.Text + "','" + txtMaxHalf.Text + "','"
                                        + txtMaxHalf.Text + "'," + iOverlapped.ToString() + ",'"+txtMaxTime.Text+"')";
                   sqlCmd.Connection = sqlCon;
                   sqlCon.Open();
                   sqlCmd.ExecuteNonQuery();
                   sqlCon.Close();
                   this.shiftDetailsTableAdapter.Fill(this.shiftDetailsDataset.ShiftDetails);
                   groupBox1.Visible = false;
                   MessageBox.Show("Successfuly Inserted");
               }
               else
               {
                   //update the shift details
                   while (shiftView.Rows.Count > 1)
                       shiftView.Rows.RemoveAt(0);
                   sqlCmd.CommandText = "update  shiftdetails set shname='" + txtShiftName.Text + "',shdesc='" + txtShiftDesc.Text
                                       + "' ,shstarttime='" + StartDate.TimeOfDay + "',shendtime='"
                                       + EndDate.TimeOfDay
                                       + "' ,shgracetime='" + txtGraceTime.Text + "',ShMinTimeFrHalfDay='"
                                       + txtMinHalf.Text + "',ShMinTimeFrFullDay='" + txtMaxHalf.Text +"',ShMaxTime='"+txtMaxTime.Text+"' where shid='"
                                       + txtShiftId.Text + "'";
                   sqlCmd.Connection = sqlCon;
                   sqlCon.Open();
                   sqlCmd.ExecuteNonQuery();
                   sqlCon.Close();
                   this.shiftDetailsTableAdapter.Fill(this.shiftDetailsDataset.ShiftDetails);
                   MessageBox.Show("Successfuly Updated");
               }
               
               ClearControls();
           }
           
        }

        // Function name   : btnClose_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/22/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }      

        
    }
}
